package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.entity.Dict;
import com.atguigu.mapper.DictMapper;
import com.atguigu.service.DictService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DictServiceImpl implements DictService {

    @Resource
    private DictMapper dictMapper;

    @Override
    public List<Map<String, Object>> findZnodes(Long id) {
        //根据父节点id查询List<Dict>
        List<Dict> dictList = dictMapper.findListByParentId(id);

        List<Map<String, Object>> znodes = new ArrayList<>();
        for (Dict dict : dictList) {
            Map<String, Object> hashMap = new HashMap<>();
            hashMap.put("id", dict.getId());
            hashMap.put("name", dict.getName());
            hashMap.put("isParent", dictMapper.countIsParent(dict.getId()) > 0);

            znodes.add(hashMap);
        }
        return znodes;
    }

    @Override
    public List<Dict> findDictListByParentDictCode(String parentDictCode) {
        return dictMapper.findDictListByParentDictCode(parentDictCode);
    }

    @Override
    public List<Dict> findDictListByParentId(Long parentId) {
        return dictMapper.findListByParentId(parentId);
    }
}
